<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Default Kohana controller. This controller should NOT be used in production.
 * It is for demonstration purposes only!
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class Welcome_Controller extends Template_Controller {

	// Disable this controller when Kohana is set to production mode.
	// See http://docs.kohanaphp.com/installation/deployment for more details.
	const ALLOW_PRODUCTION = FALSE;

	// Set the name of the template to use
	public $template = 'layouts/template_hongshaw';

	public function index()
	{
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('main');
		$this->template->keyword="";
		//sakorn.s
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->hongshaw_count=ORM::factory("hongshaw")->count_all();
		$this->template->content->keyword='';
		
		$total_items = ORM::factory("hongshaw")
	    					->find_all()
	    					->count();
	     $pagination = new Pagination(array(
	        'base_url'    => 'welcome/index/',
	        'uri_segment' => 'index', 
	        'total_items' => $total_items,
	     	'items_per_page' => 10
	    ));
	    $this->template->content->hongshaws =ORM::factory("hongshaw")
	    											->limit($pagination->items_per_page,$pagination->sql_offset)
	    											->orderby("post_date","asc")
	    											->find_all(); 
	    											
	    $this->template->content->pagination=$pagination;	
	    $this->template->title = '';

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}

	public function findByCatProduct($id){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('main');
		//sakorn.s
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->keyword='';
		$this->template->keyword="";
		
		$total_items = ORM::factory("hongshaw")
							->where("category_id",$id)
	    					->find_all()
	    					->count();
	    $this->template->content->hongshaw_count=$total_items;
	    
	    $pagination = new Pagination(array(
	        'base_url'    => 'welcome/findByCatProduct/',
	        'uri_segment' => 'findByCatProduct', 
	        'total_items' => $total_items,
	     	'items_per_page' => 10
	    ));
	    $this->template->content->hongshaws =ORM::factory("hongshaw")->where("category_id",$id)
	    											->limit($pagination->items_per_page,$pagination->sql_offset)
	    											->orderby("post_date","asc")
	    											->find_all(); 
	    											
	    $this->template->content->pagination=$pagination;	
	    $this->template->title = '';
	    
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	
	public function findDetail($id){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('hongshaw_detail');
		//sakorn.s
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->content->hongshaw =ORM::factory("hongshaw")->where("id",$id)->find();
		$hongshaw_id=$this->template->content->hongshaw->id;
		$this->template->content->hongshaw_images=ORM::factory("hongshaw_image")->where("hongshaw_id",$hongshaw_id)->find_all();
		$this->template->content->keyword='';
		$this->template->keyword="";
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	public function search($page_no){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('welcome_content');
		$keyword="";
		//sakorn.s
		$this->template->content->categorys=ORM::factory("category")->find_all();
		if($page_no!=0){
			$keyword=Session::instance()->get("kwsearch");
		}else{
			$keyword = $this->input->post("keyword");
			Session::instance()->set("kwsearch",$keyword);
		}
//		if(Session::instance()->get("kwsearch")!=null){
//			$keyword=Session::instance()->get("kwsearch");
//		}else{
//			$keyword = $this->input->post("keyword");
//			Session::instance()->set("kwsearch",$keyword);
//		}
		$this->template->keyword=$keyword;
		$total_items = Database::instance()->count_records("products","name like '%".$keyword."%'");
	    $this->template->content->product_count=$total_items;
	    $this->template->content->keyword=$keyword;
	    
	    $pagination = new Pagination(array(
	        'base_url'    => 'welcome/search/',
	        'uri_segment' => 'search', 
	        'total_items' => $total_items,
	    	'items_per_page' => 10
	    ));
	    $sql = "select * from products where 1=1  and name like '%".$keyword."%'";
	    
	    $products = Database::instance()->getwhere("products",
	    									"name like '%".$keyword."%'",
	    									$pagination->items_per_page,$pagination->sql_offset)->as_array();
	    $this->template->content->products =$products; 
	    											
	    $this->template->content->pagination=$pagination;	
	    $this->template->title = '';
	    
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';
		
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;

		// By defining a __call method, all pages routed to this controller
		// that result in 404 errors will be handled by this method, instead of
		// being displayed as "Page Not Found" errors.
		echo 'This text is generated by __call. If you expected the index page, you need to use: welcome/index/'.substr(Router::$current_uri, 8);
	}

} // End Welcome Controller